<?php
/*
 * Copyright 2009 Eat Local Food, LLC
 * Copyright (c) 2007 osCommerce (this file was written after
 * code review of osCommerce).
 *
 * This file is part of gwtCommerce.
 *
 * gwtCommerce is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 2 of the License, or
 * (at your option) any later version.
 *
 * gwtCommerce is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with gwtCommerce.  If not, see <http://www.gnu.org/licenses/>.
 */

define('IN_ADMIN_COMPONENT_CONTROLLER', 'true');

require_once('includes/functions/configuration_functions.php');

class SpecialsComponent
{
	function SpecialsComponent()
	{
	}

	function action(&$params, $action)
	{
		if ($action == 'Update') 
			return $this->edit($params);
		else if ($action == 'ToggleStatus') 
			return $this->toggleStatus($params);
		else
			return $this->query($params);
	}

	function query(&$params) 
	{
		$handle = get_class($this);

		$languageId = (int)$params['languageId'];

		$paginate = (boolean)$params['paginate'];
		$max_rows = (int)$params['max_rows'];
		$offset = (int)$params['offset'];
		$edit = (boolean)$params['edit'];

		$subParams['handle'] = $handle;
		$subParams['languageId'] = $languageId;
		$subParams['max_rows'] = $max_rows;
		$subParams['offset'] = $offset;
		$subParams['searchSpecials'] = true;
		$subParams['include_all_status'] = true;
		$subParams['return_page_info'] = $paginate;

		require_once('includes/components/ProductsSearchSubComponent.php');
		$subComponent = new ProductsSearchSubComponent();
		$value = $subComponent->query($subParams);

		if ($edit) product_list(&$value, $handle, $languageId);

		return $value;
	}

	function edit(&$params) 
	{
		$languageId = (int)$params['languageId'];
		$i = 0;
		while (true)
		{
			$i++;
			if ($i > 1000) break;

			$specialId = (int)$params['specialId-'.$i];
			if ($specialId == 0) break;

			$productId = (int)$params['specialProductId-'.$i];

			$specialPrice = (double)$params['specialPrice-'.$i];
			if ($specialPrice == 0.0) continue;

			$expiresDate = (int)$params['specialExpiresDate-'.$i];

			$status = (int)$params['specialStatus-'.$i];

			$deleteRow = (boolean)$params['specialDelete-'.$i];

			if ($deleteRow) {
				if ($specialId < 1) continue;
				$sql = 'delete from ' . TABLE_SPECIALS . ' where specials_id=' . $specialId;
				mysql_query($sql) or die(mysql_error());
			}
			else {
				$sql_data_array = array();
				$sqlAction = 'insert';
				$where = '';
				if ($specialId > 0) {
					$sqlAction = 'update';
					$where = 'specials_id=' . $specialId;
				}
				else {
					$sql_data_array['specials_date_added'] = 'now()';
				}
				//tbd: can't insert an existing product id
				if ($productId > 0) $sql_data_array['products_id'] = $productId;
				$sql_data_array['specials_new_products_price'] = $specialPrice;
				$sql_data_array['specials_last_modified'] = 'now()';
				$sql_data_array['status'] = $status;
				db_perform(TABLE_SPECIALS, $sql_data_array, $sqlAction, $where);
				
				if ($sqlAction == 'insert')
				{
					$specialId = mysql_insert_id();
				}

				if ($expiresDate > 0) {
					$expiresDate_sec = $expiresDate/1000.00;
					$sql = 'update ' . TABLE_SPECIALS . ' set expires_date = \'' . date('Y-m-d',$expiresDate_sec) . '\' where specials_id=' . $specialId;
					mysql_query($sql) or die(mysql_error());
				}
				else {
					$sql = 'update ' . TABLE_SPECIALS . ' set expires_date = null where specials_id=' . $specialId;
					mysql_query($sql) or die(mysql_error());
				}
			}
			remove_cache($languageId . '_' . $productId . '_true', 'ProductComponent');
			remove_cache($languageId . '_' . $productId . '_false', 'ProductComponent');
		}

		return $this->query($params);
	}

	function toggleStatus(&$params) 
	{
		$specialId = (int)$params['specialId'];
		$sqlQuery = 'select status from ' . TABLE_SPECIALS . ' where specials_id=' . $specialId;
		$dataReturned = mysql_query($sqlQuery) or die(mysql_error());
		
		while($row = mysql_fetch_array($dataReturned))
		{
			$status = (int)$row['status'];
			if ($status != 0) $status = 0;
			else $status = 1;
			$sql = 'update ' . TABLE_SPECIALS . ' set status=' . $status . ', specials_last_modified=now() where specials_id=' . $specialId;
			mysql_query($sql) or die(mysql_error());
			break;
		}
		return $this->query($params);
	}
}
?>
